<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="退货编号" prop="rtvId">
        <el-input
          v-model="queryParams.rtvId"
          placeholder="请输入退货编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退货订单" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请选择退货订单"
          clearable
        />
      </el-form-item>
      <el-form-item label="退货状态" prop="rtvStatus">
        <el-select
          v-model="queryParams.rtvStatus"
          placeholder="选择退货状态"
          clearable
          @keyup.enter="handleQuery"
        >
          <el-option
            v-for="(sta, index) in states"
            :key="index"
            :label="sta"
            :value="index"
        /></el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格展示 -->
    <el-table
      v-loading="loading"
      :data="returnto"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="退货编号"
        width="100"
        align="center"
        prop="rtvId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="订单编号"
        align="center"
        prop="orderId"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="退货原因"
        align="center"
        prop="rtvBecause"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="退货时间"
        align="center"
        prop="rtvTime"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="退货状态"
        align="center"
        prop="rtvStatus"
        :formatter="StatusFormatter"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-tooltip
            content="查看"
            placement="top"
            v-if="scope.row.rtvStatus == '1' || scope.row.outStoState == '2'"
          >
            <el-button
              type="text"
              icon="Search"
              @click="handleUpdate(scope.row)"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="查看" placement="top" v-else>
            <el-button
              type="text"
              icon="Search"
              @click="handleUpdate(scope.row)"
              >查看</el-button
            >
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.page"
      v-model:limit="queryParams.count"
      @pagination="getList"
    />

    <!-- 修改审批表状态对话框 -->
    <el-dialog
      v-if="checkopen"
      :title="title"
      v-model="checkopen"
      width="800px"
      append-to-body
    >
      <el-form ref="jobRef" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="退货编号" prop="rtvId">
              <el-input
                v-model="form.rtvId"
                aria-readonly="true"
                disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退货原因" prop="rtvBecause">
              <el-input
                v-model="form.rtvBecause"
                placeholder="请选择出库的仓库"
                disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="退货时间" prop="rtvTime">
              <el-input v-model="form.rtvTime" disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div>
        <!-- 表格头部 -->
        <el-row :gutter="20">
          <el-col :span="8"
            >客户：{{ form.order.customer.customerName }}</el-col
          >
          <el-col :span="8"
            >销售员：{{ form.order.salesPerson.userName }}</el-col
          >
        </el-row>

        <el-row :gutter="20" style="margin-top: 10px">
          <el-col :span="8"
            >状态：<span style="color: green">退货</span></el-col
          >
        </el-row>

        <el-table
          :data="form.order.orderDetails"
          border
          stripe
          style="width: 100%; margin-top: 20px"
        >
          <el-table-column label="销售订单商品信息" align="center">
            <el-table-column
              prop="id"
              label="序号"
              width="120"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="inventory.product.productId"
              label="商品编号"
              width="120"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="inventory.product.productName"
              label="商品名称"
              width="150"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="price"
              label="商品单价"
              width="80"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="total"
              label="商品数量"
              width="80"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="totalAmount"
              label="商品总价"
              width="100"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="inventory.product.brand"
              label="商品品牌"
              width="100"
              align="center"
            ></el-table-column>
            <el-table-column
              prop="inventory.product.description"
              label="商品描述"
              min-width="100"
              align="center"
            ></el-table-column>
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="return">
import {
  delReturn,
  getReturn,
  listReturn,
  updateReturn,
} from "@/api/sale/returnto";

// const router = useRouter();
const { proxy } = getCurrentInstance();
// const { sys_job_group, sys_job_status } = proxy.useDict(
//   "sys_job_group",
//   "sys_job_status"
// );
const states = ref(["已完成"]);
const returnto = ref([]);
const checkopen = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const openView = ref(false);
const openCron = ref(false);
const expression = ref("");

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    count: 5,
    rtvId: undefined,
    orderId: undefined,
    rtvStatus: undefined,
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询定时任务列表 */
function getList() {
  loading.value = true;
  listReturn(queryParams.value).then((response) => {
    returnto.value = response.data.records;
    total.value = response.data.total;
    loading.value = false;
    console.log(returnto);
  });
}

/** 取消按钮 */
function cancel() {
  checkopen.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {};
  proxy.resetForm("jobRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.warehouseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
//修改状态
function handleUpdate(row) {
  reset();
  const rtvId = row.rtvId || ids.rtvId;
  getReturn(rtvId).then((response) => {
    form.value = response.data;
    checkopen.value = true;
    title.value = "修改退货单";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["jobRef"].validate((valid) => {
    if (valid) {
      if (form.value.rtvId !== undefined) {
        form.value.rtvStatus = 1;
        updateReturn(form.value).then((response) => {
          console.log(response);
          proxy.$modal.msgSuccess("审核成功");
          checkopen.value = false;
          getList();
        });
      }
    }
  });
}

//状态展示文字加工
function StatusFormatter(row) {
  // 确保 orderStatus 是有效的索引
  if (row.rtvStatus !== undefined && row.rtvStatus < states.value.length) {
    return states.value[row.rtvStatus];
  }
  return ""; // 或者返回一个默认值
}

/** 删除按钮操作 */
function handleDelete(row) {
  const rtvId = row.rtvId || ids.value;
  proxy.$modal
    .confirm('是否确认删除销售订单为"' + rtvId + '"的数据项?')
    .then(function () {
      return delReturn(rtvId);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

getList();
</script>
